package com.jsbs.iam.ident.controller;

import com.github.pagehelper.PageInfo;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.utils.Json;
import com.jsbs.iam.common.core.utils.poi.ExcelUtil;
import com.jsbs.iam.ident.dto.UserLogPageDto;
import com.jsbs.iam.ident.service.IamUserLogService;
import com.jsbs.iam.ident.vo.IamAccountBaseVo;
import com.jsbs.iam.ident.vo.IamLoginLogVo;
import com.jsbs.iam.ident.vo.IamUpdateUserLogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author zhouxiangyang
 * @className IamUserLogController
 * @date 2023/1/5 19:23
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/iam/ident/userLog")
public class IamUserLogController extends BaseController{

    @Autowired
    private IamUserLogService iamUserLogService;

    @RequestMapping("/getLoginLogPage")
    public Result getLoginLogPage (@RequestBody UserLogPageDto pageDto){
        try {
            PageInfo<IamLoginLogVo> loginLogPage = iamUserLogService.getLoginLogPage(pageDto);
            return Result.success(loginLogPage);
        } catch (Exception e) {
            return handleException(e,e.getMessage());
        }
    }

    @PostMapping("/userLoginLogExport")
    public void userLoginLogExport(@RequestBody UserLogPageDto pageDto, HttpServletResponse response){
        try {
            log.info("Route=>IamUserLogController method=>userLoginLogExport 前台入参:{}", Json.toJsonString(pageDto));
            List<IamLoginLogVo> loginLogList = iamUserLogService.getLoginLogList(pageDto);
            pageDto.setCurrPage(0);
            pageDto.setPageSize(loginLogList.size());
            if (CollectionUtils.isEmpty(loginLogList)) {
                throw new RuntimeException("导出数据为空！");
            }
            if (loginLogList.size() > 5000) {
                throw new RuntimeException("导出数据超5千条，请分批次导出！");
            }
            long currentTimeMillis = System.currentTimeMillis();
            ExcelUtil<IamLoginLogVo> util = new ExcelUtil<IamLoginLogVo>(IamLoginLogVo.class);
            String title="登录日志记录表"+currentTimeMillis+".xlsx";
            String excelFileName="登录日志记录表"+currentTimeMillis+".xlsx";
            util.exportExcel(response,loginLogList, "登录日志记录表",title,excelFileName);
            log.info("Route=>IamUserLogController method=>userLoginLogExport 导出成功！");
        }catch (Exception e) {
            log.error("用户登录日志导出失败，错误信息为{}",e);
            e.printStackTrace();
        }
    }

    @RequestMapping("/getUpdateUserLogPage")
    public Result getUpdateUserLogPage (@RequestBody UserLogPageDto pageDto){
        try {
            PageInfo<IamUpdateUserLogVo> updateUserLogPage =
                    iamUserLogService.getUpdateUserLogPage(pageDto);
            return Result.success(updateUserLogPage);
        } catch (Exception e) {
            return handleException(e,e.getMessage());
        }
    }

    @PostMapping("/updateUserLogExport")
    public void updateUserLogExport(@RequestBody UserLogPageDto pageDto, HttpServletResponse response){
        try {
            log.info("Route=>IamUserLogController method=>updateUserLogExport 前台入参:{}", Json.toJsonString(pageDto));
            List<IamUpdateUserLogVo> updateUserLogList = iamUserLogService.getUpdateUserLogList(pageDto);
            pageDto.setCurrPage(0);
            pageDto.setPageSize(updateUserLogList.size());
            if (CollectionUtils.isEmpty(updateUserLogList)) {
                throw new RuntimeException("导出数据为空！");
            }
            if (updateUserLogList.size() > 5000) {
                throw new RuntimeException("导出数据超5千条，请分批次导出！");
            }
            long currentTimeMillis = System.currentTimeMillis();
            ExcelUtil<IamUpdateUserLogVo> util = new ExcelUtil<IamUpdateUserLogVo>(IamUpdateUserLogVo.class);
            String title="修改日志记录表"+currentTimeMillis+".xlsx";
            String excelFileName="修改日志记录表"+currentTimeMillis+".xlsx";
            util.exportExcel(response,updateUserLogList, "修改日志记录表",title,excelFileName);
            log.info("Route=>IamUserLogController method=>updateUserLogExport 导出成功！");
        }catch (Exception e) {
            log.error("用户登录日志导出失败，错误信息为{}",e);
            e.printStackTrace();
        }
    }

}
